package com.shop.merchant.handler;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.shop.merchant.Interceptor.GetMerchantInfoInterceptor;
import com.shop.merchant.po.Merchant;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @author 'banxia'
 * @version 1.0
 * @description TODO
 * @date 2023/2/23 21:21
 */
@Component
public class MyMetaObjectHandler implements MetaObjectHandler {
    @Override
    public void insertFill(MetaObject metaObject) {
        //参数1：对应类中的属性名称
        this.setFieldValByName("createTime", new Date(), metaObject);
        this.setFieldValByName("updateTime", new Date(), metaObject);
        Merchant merchant = GetMerchantInfoInterceptor.getMerchant();
        if (merchant==null){
            merchant=new Merchant();
            merchant.setId(0L);//
        }
        this.setFieldValByName("createUser", merchant.getId(), metaObject);
        this.setFieldValByName("updateUser", merchant.getId(), metaObject);
    }
    @Override
    public void updateFill(MetaObject metaObject) {
        Merchant merchant = GetMerchantInfoInterceptor.getMerchant();
        this.setFieldValByName("updateTime", new Date(), metaObject);
        this.setFieldValByName("updateUser", merchant.getId(), metaObject);
    }
}
